#!/bin/sh

# This script is used to launch (busybox) init on WSL2.  This is necessary as
# we need to do some mount namespace shenanigans, since we store the data on the
# WSL shared mount (/mnt/wsl/rancher/desktop/) and that can have issues with
# lingering tmpfs mounts after we exit.  This means we need to run this script
# under unshare (to get a private mount namespace), and then we can mark various
# mount points as shared (for buildkit).  Kubelet will internally do some
# tmpfs mounts for volumes (secrets, etc.), which will stay private and go away
# once k3s exits, so that we can delete the data as necessary.

set -o errexit -o nounset -o xtrace

if [ $$ -ne "1" ]; then
    # This is not running as PID 1; this means that this is a normal invocation
    # from WSL.  Set up the namespaces, and try again.
    echo $$ > /run/wsl-init.pid
    exec /usr/bin/unshare --pid --mount-proc --fork --propagation slave "${0}"
fi

# Mark directories that we will need to bind mount as shared mounts.
(
    IFS=:
    for dir in / ${DISTRO_DATA_DIRS}; do
        mount --make-shared "${dir}"
    done
)

# Mount bpffs to allow containers to leverage bpf, and make both bpffs and
# cgroupfs shared mounts so the pods can mount them correctly.
mount bpffs -t bpf /sys/fs/bpf
mount --make-shared /sys/fs/bpf
mount --make-shared /sys/fs/cgroup

# Mount binfmt_misc to allow nerdctl to see which qemu-* handlers have been loaded.
# It will display a warning for foreign platforms if their handler seems missing.
mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc
mount --make-shared /proc/sys/fs/binfmt_misc

if [ -f /var/lib/resolv.conf ]; then
    ln -s -f /var/lib/resolv.conf /etc/resolv.conf
fi

# Run init (which never exits).
exec /sbin/init
